package ua.pp.bizon.cripto.configuration;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ua.pp.bizon.cripto.file.FileFactory;
import ua.pp.bizon.cripto.file.IPath;
import ua.pp.bizon.cripto.keystore.CryptoUtil.Direction;
import ua.pp.bizon.cripto.utils.FtpConfig;

public class Configuration {

    private static final String ARGS_FROM = "from=";
    private static final String ARGS_FROM_FILE = "read_config=";
    private static final String ARGS_TO = "to=";
    private static final String ARGS_FTP = "ftp=";
    private static final String ARGS_OVERWRITE = "overwrite=";
    // private static final String ARGS_DIRECTION = "direction=";
    private static final String ARGS_DIRECTION_ENCODE = "direction=encode";
    private static final String ARGS_DIRECTION_DECODE = "direction=decode";

    private static final Log log = LogFactory.getLog(Configuration.class);

    public static Configuration load(String[] params) {
        Configuration c = new Configuration();
        for (String s : params) {
            log.debug("processing param:" + s + ":");
            if (s.startsWith(ARGS_FROM)) {
                c.from = FileFactory.parse(s.substring(ARGS_FROM.length()));
            } else if (s.startsWith(ARGS_TO)) {
                c.to = FileFactory.parse(s.substring(ARGS_TO.length()));
            } else if (s.startsWith(ARGS_FROM_FILE)) {
                try {
                    c = load(s.substring(ARGS_FROM_FILE.length()));
                } catch (IOException e) {
                    log.error("Can't read the configuration from " + s.substring(ARGS_FROM_FILE.length() + 1) + ", cause:" + e.getMessage(), e);
                }
            } else if (s.startsWith(ARGS_FTP)) {
                c.configName = s.substring(ARGS_FTP.length());
            } else if (s.startsWith(ARGS_OVERWRITE)) {
                c.override = Boolean.parseBoolean(s.substring(ARGS_OVERWRITE.length()));
            } else if (s.startsWith(ARGS_DIRECTION_DECODE)) {
                c.direction = Direction.DECODE;
            } else if (s.startsWith(ARGS_DIRECTION_ENCODE)) {
                c.direction = Direction.ENCODE;
            } else
                log.warn("unknown parameter: " + c);
        }
        return c;
    }

    public static Configuration load(String path) throws IOException {
        return load(FileUtils.readLines(new File(path)).toArray(new String[0]));
    }
    
    public Configuration() {
    }
    
    public Configuration(IPath from, IPath to, Direction direction) {
        super();
        this.from = from;
        this.to = to;
        this.direction = direction;
    }



    private IPath from;
    private IPath to;
    private boolean override = true;
    private String configName;
    private FtpConfig ftpConfig;
    private Direction direction;

    public IPath getFrom() {
        return from;
    }

    public void setFrom(IPath from) {
        this.from = from;
    }

    public IPath getTo() {
        return to;
    }

    public void setTo(IPath to) {
        this.to = to;
    }

    public boolean isOverride() {
        return override;
    }

    public String getConfigName() {
        return configName;
    }

    public FtpConfig getFtpConfig() {
        if (ftpConfig == null) {
            try {
                ftpConfig = FtpConfig.readConfig(configName);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return ftpConfig;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }
}
